package www.lagou.Dao;

import org.apache.commons.dbutils.QueryRunner;
import www.lagou.utils.DruidUtils;

import java.io.ObjectInputStream;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TransactionDao {
    /**
     * 创建转入和转出记录到Transaction表中
     */
    public boolean writeTransLog(String fromCard, String toCard, Double tranQty) {
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());//设置自动创建连接模式
            //定义日期格式，要求匹配mysql的datetime格式
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //创建sql语句插入两条数据行，分别是转账出户和转账入户对应的转账记录
            String sql1 = "insert into Transaction Values(null,?,'转出',?,?);";
            String sql2 = "insert into Transaction Values(null,?,'转入',?,?);";
            //将待传入的参数分别封装到object对象数组中
            Object[] param1 = {fromCard, tranQty, df.format(new Date())};
            Object[] param2 = {toCard, tranQty, df.format(new Date())};
            //传入参数数组，然后分别执行sql语句的命令，记录该次转账的历史消息
            qr.update(sql1, param1);
            qr.update(sql2, param2);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
